package App.Service;

import App.Model.JPush.JPushConfig;
import App.Model.JPush.JPushCustomerContent;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.*;
import cn.jiguang.common.ClientConfig;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class JPushService {
    @Autowired
    private JPushConfig _jPushConfig;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private JPushClient _jpushClient;

    private JPushClient getJPushClient() {
        if (_jpushClient == null) {
            ClientConfig clientConfig = ClientConfig.getInstance();

            String _clientConfig = JSONObject.toJSONString(clientConfig);
            String _masterSecret = _jPushConfig.getMasterSecret();
            String _appKey = _jPushConfig.getAppKey();
            logger.info("jpush push sendPushByAlias clientConfig:" + _clientConfig);
            logger.info("jpush push sendPushByAlias masterSecret :" + _masterSecret);
            logger.info("jpush push sendPushByAlias appKey:" + _appKey);
            // 极光推送
            _jpushClient = new JPushClient(_masterSecret, _appKey, null, clientConfig);
        }

        return _jpushClient;
    }

    public void sendPushByAliasCustomer(Collection<String> alias, String title, String content, JPushCustomerContent customerContent) {
        JPushClient jpushClient = getJPushClient();

        HashMap<String, String> extras = new HashMap<String, String>();
        if (customerContent != null) {
            extras.put("key", customerContent.getKey());
            extras.put("projectid", customerContent.getProjectid());
            extras.put("taskid", customerContent.getTaskid());
            extras.put("pushType", customerContent.getPushType());
        }
        final PushPayload payload = buildPushObject_android_and_ios_alias(alias, title, content, extras);
        try {
            boolean _isSendmsg = _jPushConfig.getSendmsg();
            logger.info("jpush push sendPushByAlias isSendmsg:" + _isSendmsg);
            if (_isSendmsg) {
                PushResult result = jpushClient.sendPush(payload);
                logger.info("jpush push response success:" + result);
            }
        } catch (Exception e) {
            logger.info("jpush push response fail:" + e.getMessage());
        }
    }

    public void sendPushByAlias(Collection<String> alias, String title, String content, HashMap<String, String> extras) {
        JPushClient jpushClient = getJPushClient();
        final PushPayload payload = buildPushObject_android_and_ios_alias(alias, title, content, extras);
        try {
            boolean _isSendmsg = _jPushConfig.getSendmsg();
            logger.info("jpush push sendPushByAlias isSendmsg:" + _isSendmsg);
            if (_isSendmsg) {
                PushResult result = jpushClient.sendPush(payload);
                logger.info("jpush push response success:" + result);
            }
        } catch (Exception e) {
            logger.info("jpush push response fail:" + e.getMessage());
        }
    }

    public PushPayload buildPushObject_android_and_ios_alias(Collection<String> alias, String title, String content, HashMap<String, String> extras) {
        JsonObject intent = new JsonObject();
        String intentStr = _jPushConfig.getAppintent();
        intent.add("url", new JsonPrimitive(intentStr));

        //Jpush 本身有bug inbox必须有值 intent才会生效
        JsonObject inbox = new JsonObject();

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle(title)
                                .addExtras(extras)
                                .setInbox(inbox)
                                .setChannelId("new_jpush")
                                .setIntent(intent).build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtras(extras).build())
                        .build())
                .build();
    }
}
